package com.xuecheng.content.api;

import com.xuecheng.base.exception.ValidationGroups;
import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.content.model.dto.AddCourseDto;
import com.xuecheng.content.model.dto.CourseBaseInfoDto;
import com.xuecheng.content.model.dto.EditCourseDto;
import com.xuecheng.content.model.dto.QueryCourseParamsDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.service.CourseBaseInfoService;
import com.xuecheng.utils.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author Tang
 * @version 1.0
 * @date 2023/12/20 20:48
 */
@RestController
@Api(value = "课程信息编辑接口", tags = "课程信息编管理接口")
@RequestMapping("/course")
public class CourseBaseInfoController {
    private final CourseBaseInfoService courseBaseInfoService;
    public CourseBaseInfoController(CourseBaseInfoService courseBaseInfoService) {
        this.courseBaseInfoService = courseBaseInfoService;
    }

    @PostMapping("/list")
    @ApiOperation("课程查询接口")
    @PreAuthorize("hasAuthority('xc_teachmanager_course_list')")
    public PageResult<CourseBase> list(PageParams pageParams,
                                       @RequestBody(required = false) QueryCourseParamsDto queryCourseParamsDto) {
        String companyId = SecurityUtil.getUser().getCompanyId();
        companyId = (companyId == null ? "0" : companyId);
        return courseBaseInfoService.queryCourseBaseList(Long.valueOf(companyId),
                pageParams, queryCourseParamsDto);
    }

    @ApiOperation("新增课程")
    @PostMapping
    public CourseBaseInfoDto addCourse(@RequestBody
                                           @Validated(ValidationGroups.Insert.class) AddCourseDto addCourseDto) {
        return courseBaseInfoService.addCourse(1232141425L, addCourseDto);
    }

    @ApiOperation("根据查询课程信息")
    @GetMapping("/{courseId}")
    public CourseBaseInfoDto getCourse(@PathVariable("courseId") Long courseId) {
        return courseBaseInfoService.selectCourseBaseInfoById(courseId);
    }

    @ApiOperation("修改课程")
    @PutMapping
    public CourseBaseInfoDto updateCourse(@RequestBody
                                              @Validated(ValidationGroups.Update.class) EditCourseDto editCourseDto) {
        return courseBaseInfoService.updateCourseBaseInfo(1232141425L, editCourseDto);
    }
}
